package com.rtx.edi.platform.dash.error.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rtx.api.annotation.DataSourceAnnotation;
import com.rtx.edi.model.base.Constant;
import com.rtx.edi.model.error.po.ApiOrders;
import com.rtx.edi.platform.dash.error.mapper.PlApiOrdersMapper;
import com.rtx.edi.platform.dash.error.service.PlApiOrdersService;
import com.rtx.wms.std.common.bean.WmsConstant;

@Service
public class PlApiOrdersServiceImpl extends ServiceImpl<PlApiOrdersMapper,ApiOrders> implements PlApiOrdersService {
	
	@DataSourceAnnotation
	@Override
	public void updateApiOrders(String whseid, ApiOrders apiOrders) {
		LambdaUpdateWrapper<ApiOrders> update =Wrappers.lambdaUpdate();
		 update.eq(ApiOrders::getSerialkey,apiOrders.getSerialkey())
		 .set(ApiOrders::getEdiflag,Constant.DEFAULT_FLAG)
		 .set(ApiOrders::getRtxqueue,"0");
		 baseMapper.update(null, update) ;
		
	}
	
	@DataSourceAnnotation
	@Override
	public void updateSuccess(String whseid, ApiOrders apiOrders) {
		LambdaUpdateWrapper<ApiOrders> update =Wrappers.lambdaUpdate();
		 update.eq(ApiOrders::getSerialkey,apiOrders.getSerialkey())
		 .set(ApiOrders::getEdiflag,Constant.SUCCESS_FLAG)
		 .set(ApiOrders::getNotes,new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(new Date())+"手动标记为成功")
		 .set(ApiOrders::getRtxqueue,0);
		 baseMapper.update(null, update) ;
		
	}



	@DataSourceAnnotation
	@Override
	public void selectApiOrdersErrorList(String datasourcekey,Page<ApiOrders> pageParam, ApiOrders error) {
		LambdaQueryWrapper<ApiOrders> query =Wrappers.lambdaQuery();
		query.eq(ApiOrders::getEdiflag,WmsConstant.FAIL_FLAG).or().gt(ApiOrders::getRtxqueue, WmsConstant.DEFAULT_COUNT);
		
		if(error!=null) {
			query.eq( error.getExternorderkey()!=null,ApiOrders::getExternorderkey, error.getExternorderkey())
			.eq( error.getSerialkey()!=null,ApiOrders::getSerialkey, error.getSerialkey())
			.eq( error.getWhseid()!=null,ApiOrders::getWhseid, error.getWhseid()) ;
		}
		baseMapper.selectPage(pageParam,query);
	}
}
